For more info about OneClick, please go to the following web pages.
 
 
Mark Brooks can be reached at:
<mbrooks@westcodesoft.com>
Editor's Note: The following script is using TexEdit. You can apply this script to many different programs. WestCode Software maintains a very active mailing list to help people in using OneClick. You can subscribe to the mailing list at:
I received some nice responses to the first tutorial. Thanks to everyone who wrote. This week I'll explain how to edit a recorded macro. There will be two changes made to last issue's macro. The first is relatively easy, while (that's the keyword of this tutorial, by the way) the second will introduce some standard concepts used when writing more complex macros. Don't worry, the concepts are not that difficult and they contain some of the building blocks used to build macros that batch process files, an eventual topic of the tutorial series.
To help explain the purpose of this series, I'll repeat something from the first tutorial:
These tutorials are my attempt to show you what you can have OneClick do for you in a practical, everyday way. I welcome your responses, questions and suggestions, praise and criticism. In fact, I would like to add a question/answer section to the tutorials, space and interest permitting. There were not any questions sent in from last week, so there will not be a Questions and Answers section this issue.
Editor's Note: I will gladly devote some space in the newsletter and on my web sites to the Questions and Answers section mentioned above if there is interest. Please send email to Mark Brooks and myself if you would like to see this.
Section 2 - This Issue's Tutorial "Editing Recorded Scripts"
In the first tutorial, we created a simple macro for Tex-Edit Plus. The macro selected "Save As…" from the "File" menu and then changed the format to "Simple Text Read Only." Let's get that button out of the mothballs and work with it again today.
This time, we will further automate the "save as" process so that it gives the file a unique name and saves it to a specific folder. This will require using new commands and expressions in OneClick: Type, File, While, Dialogs and Directory.
To start with, you will need to create a folder on your desktop to save Tex-Edit files into. It does not matter where it is, just that you know where it is for step 2 of the tutorial. My folder will be named "TexEdit in progress" and will be located on the desktop.
Step 1 - Open the macro in the Script Editor
With Tex-Edit Plus running, open the OneClick editor and select the palette tab. Select the palette that has our original macro on it. Then, select the button by clicking on it and then click on the "Script" tab in the editor. You should see this picture from last month's tutorial.
 
Step 2 - Add in the Directory command
In OneClick, you can set the directory (folder) that appear's in an applications Open/Save dialog box. At the beginning of the script, type:
Directory =
and then select "File" from the "Parameters" pop-up menu.
 
Find the folder you want to use, select it, and then click the button at the bottom of the dialog. Afterwards, you should end up with a script that looks like this.
 
If you were to run the script now, you would see the same thing as last week, except that the Open/Save dialog box opens to that specific folder. An important thing to note is that the Directory command is treated differently by some applications. With some, you need to set the Directory after the Open/Save dialog box appears and with other, like Tex-Edit Plus, you need to set it before.
For example, if you have a program where you need to set the Directory command after the Open/Save dialog box, it would look like this:
Directory="MacAssistant:Adobe Illustrator® 7.0:"
SelectMenu "File", "Save As..."
Step 3 - Create the While loop
This next step is going to introduce some new scripting concepts. In order to have a unique file name, so that we do not lose past work, we will need to loop through file names until ours is unique. The easiest way to do this is to serialize the names, or add an index number to the end of the name so that we can change it easily.
A While loop is a way to tell the computer to repeat a step while some condition is true. In this case, we want it to repeatedly check a file name until it finds one that does not exist. Then we can assign that name to our own file without any risk.
Step 3a - Declare a variable
At the beginning of the macro, before the Directory command, we will need to tell OneClick that we need a variable. A variable is any word that the user can use to store information in, be it text, numbers, snail mail addresses, or the recipe for fortune cookies. For us, this "loop variable" will keep track of the times we have gone through the loop. We'll use the loop variable to serialize the file names. For simplicity's sake, let's use "loopcount." You can use any term for your variable name. I would suggest that you make it descriptive so you can tell later what the variable's purpose is. So, you will type in:
Variable loopcount
Your script should now look like this figure.
 
Step 3b - Checking to see if a file exists
The way to see if a file exists in OneClick is to use the File object. Objects in OneClick are combined with another word to get a specific property or quality about the object. In this case, we're going to use File.Exists to test if the file exists. Also, with objects in OneClick, you need to specify the specific object you are working with. To do this, you place the path to the file inside of the File.Exists statement. You end up with:
File(path-name).Exists
By the way, now may be a good time to mention that the OneClick editor has a built-in glossary as shown below, of every term used in EasyScript. EasyScript, you'll remember, is the language that OneClick uses to record its macros. To access the built-in glossary, just click the small icon on the left side of the window that looks like a small, open book.
 
Now the file we want to check is a file with a specific name combined with the loopcount variable. In this case, we will simply use "TempFile" as the starting name. So then, the name of the file itself would be:
"TempFile" & loopcount
When we combine that with the folder that the file will be stored in, we get:
Directory & "TempFile" & loopcount
That is what goes in the File.Exists statement. We end up with the complex statement:
File(Directory & "TempFile" & loopcount).Exists
Whew! That's a long step. Now, this File.Exists statement returns either a "true" or a ":false" depending on whether or not there really is a file with that path and that name.
Step 3c - Placing the While Loop
Now, we can write the shell of the While loop:
While File(Directory & "TempFile" & loopcount).Exists
End While
Now, if the file exists, as we said above, we want to change the name of the file by changing the value of loopcount. So, we simply add one to loopcount. This is written as:
loopcount = loopcount + 1
Every time OneClick sees this line, loopcount will be increased. This becomes the body of the While loop:
While File(Directory & "TempFile" & loopcount).Exists
loopcount = loopcount + 1
End While
At this point, it does not matter where we put the While loop. It only needs to occur before we type out the name of the file into the dialog and after the variable declaration and the setting of the directory. It needs to go after the Directory statement since we need to take the folder we're working with into account when checking file names.
I'll place mine directly after the Directory line.
 
Step 4 - Typing the file name
At the end of the current macro, after we set the directory, find a unique file name, select "Save As…", and change it to "Simple Text Read Only" (we've done a lot with this already!), we need to give the file a name by typing it into the "Save document as:" field.
You'll notice that, when you first open the "Save As…" box, that field is selected. This means that you will not need to change fields by using tab or some other shortcut. You can just begin to type. In OneClick, we use the Type command. In this case, we need to type text, a string of letters, and then type the value of a variable, loopcount.
We can do this in two steps like so:
Type "TempFile"
Type loopcount
Note that to type the contents of the variable, you do not use quotation marks. To type characters, you do use quotation marks. Also, you can combine multiple Type statements by using parenthesis:
Type ("TempFile" & loopcount)
 
Step 5 - exiting the dialog
There are many ways to do this, including clicking directly on the button. However, I prefer to use keyboard commands whenever possible. So, to exit the dialog, we need to simply press the return key. In OneClick, some keys (return, delete, tab, etc.) have special symbols. You can get those symbols by holding down option while typing the key. So, we will end up with:
Type "<return>"
where <return> is the special arrow symbol for return. You can, by the way, also use the text equivalent above...all special characters have text equivalents, usually the name of the key in between the less than and greater than signs.
In the script editor, you now have something that looks like this.
 
In this case, it is not a good idea to combine the last Type statement with the previous one. The name of the file needs to be typed separately so that it can be updated *before* you exit the dialog by typing return. If this goes too fast, the file name is not changed.
The corrected script should look like this:
 
Step 6 - I don't like that dialog, Mark
When you run the macro, the Save As dialog does appear on the screen briefly. It looks like this. This can be distracting when you just want it to save the file so that you can get back to work. The way around this is to use the Dialogs command in OneClick. By setting
Dialogs = 0
OneClick will hide the dialogs from you while the macro is in progress. If this is placed before we call the dialog, we will be in the clear. The final product looks like this.
 
Section 3 - The end, my friend
Well, this tutorial was a tad longer and a bit more in depth. I hope that it helps you understand some of the power of OneClick to make ordinary tasks much simpler. With only one or two more elements, you will be able to batch convert files to belong to a specific application. If there are not too many questions, that will probably be the topic of the next tutorial. If you do have a question, you're probably not alone. Please send them on to me and I'll answer them, both to your privately, and then in general in the next tutorial.
If you will notice in the final script, I have separated each of the major portions of the script with a blank line. This makes the scripts much easier to read laterif you need to modify the script.